﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Stub;
using Nvigorate.Relational.Mapping;
using Nvigorate.Relational.Mapping.Interface;

namespace Nvigorate.Test.Relational.Binding
{
    [TestClass]
    public class BindingMapBaseTests
    {
        #region Fields

        private const string ELEMENT_DEFAULT = "<map repositoryName=\"tableA\" target=\"classA\" />";
        private const string ELEMENT_SOURCES = "<map repositoryName=\"tableA\" target=\"classA\" ><source name=\"source1\" /><source name=\"source2\" /></map>";

        #endregion
        #region ParseXElement Tests

        [TestMethod]
        public void ParseXElement_DefaultValues_PopulatesObject()
        {
            // Act
            var actual = new ConcreteBindingMap();
            actual.ParseXElement(XElement.Parse(ELEMENT_DEFAULT));

            // Assert
            Assert.AreEqual("tableA", actual.RepositoryName);
            Assert.AreEqual("classA", actual.TargetName);
        }

        [TestMethod]
        public void ParseXElement_WithSources_PopulatesSources()
        {
            // Act
            var actual = new ConcreteBindingMap();
            actual.ParseXElement(XElement.Parse(ELEMENT_SOURCES));

            // Assert
            Assert.AreEqual(2, actual.Sources.Count);
            Assert.AreEqual("source1", actual.Sources["source1"].SourceName);
            Assert.AreEqual("source2", actual.Sources["source2"].SourceName);
        }

        #endregion

        #region Nested Types
        private class ConcreteBindingMap : BindingMapBase
        {

            public override bool Equals(object obj)
            {
                return base.Equals(obj as BindingMapBase);
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }

            protected override ISourceMap GetSourceMap(XElement element)
            {
                var source = new Mock<ISourceMap>();
                var nameAttribute = element.Attribute("name");
                source.Stub(s => s.SourceName,  nameAttribute == null ? "" : nameAttribute.Value);
                return source.Object;
            }
            
        }
        #endregion
    }
}
